package cn.com.haopy.dataes.db.service.hos.impl;

import cn.com.haopy.dataes.db.entity.hos.HosDrug;
import cn.com.haopy.dataes.db.mapper.hos.HOSDrugMapper;
import cn.com.haopy.dataes.db.service.hos.HDAdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HDAdminServiceImpl extends ServiceImpl<HOSDrugMapper, HosDrug> implements HDAdminService {
    @Override
    public List<HosDrug> findByName(String name) {
        LambdaQueryWrapper<HosDrug> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HosDrug::getName, name);

        return super.list(queryWrapper);
    }

    @Override
    public Page<HosDrug> querySelective(String name, Integer pageNo, Integer size, String sort, String order) {

        QueryWrapper<HosDrug> queryWrapper = new QueryWrapper<>();

        if (StringUtils.hasText(name)) {
            queryWrapper.lambda().like(HosDrug::getName, "%" + name + "%");
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }

        Page<HosDrug> page = new Page<>(pageNo, size);

        return super.page(page, queryWrapper);
    }

}
